package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.typeTemplate.TbTypeTemplate;
import com.dongdongshop.service.BrandServiceI;
import com.dongdongshop.service.SpecificationServiceI;
import com.dongdongshop.service.TemplateServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *模板管理
 */
@Controller
@RequestMapping("template")
public class TemplateController {

    private final Logger logger= LoggerFactory.getLogger(TemplateController.class);

    @Reference
    private BrandServiceI brandServiceI;
    @Reference
    private SpecificationServiceI specificationServiceI;
    @Reference
    private TemplateServiceI templateServiceI;

    @RequestMapping("showTemplate")
    public String showTemplate(Model model){
        List<TbBrand> brandList = brandServiceI.listBrands();
        List<TbSpecification> specificationList=specificationServiceI.getSpecs();

        model.addAttribute("brandAll",brandList);
        model.addAttribute("specAll",specificationList);

        return "admin/type_template";
    }

    @RequestMapping("templateList")
    @ResponseBody
    public Result<List<TbTypeTemplate>> templateList(@RequestParam("name")String name){
        try {
            List<TbTypeTemplate> templateList=templateServiceI.templateList(name);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(templateList);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("deleteTemplates")
    @ResponseBody
    public Result deleteTemplates(@RequestParam("ids[]")Integer[] ids){
        try {
            int count=templateServiceI.deleteTemplates(ids);
            if (count >0) {
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("insertTemplate")
    @ResponseBody
    public Result insertTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            templateServiceI.insertTemplate(tbTypeTemplate);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("getTempById/{id}")
    @ResponseBody
    public Result<TbTypeTemplate> getTempById(@PathVariable("id")Long id){
        try {
            TbTypeTemplate typeTemplate=templateServiceI.getTempById(id);
            System.out.println("方法id为------------"+typeTemplate.getId());
            return Result.jsonResponse(HttpResponseEnum.OK).setData(typeTemplate);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate tbTypeTemplate){
        try {
          int i= templateServiceI.updateTemplate(tbTypeTemplate);
          if (i>0) {
              return Result.jsonResponse(HttpResponseEnum.OK);
          }else {
              return Result.jsonResponse(HttpResponseEnum.FAIL);
          }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
